<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="userTelephone">
        <el-input
          v-model="queryParams.userTelephone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:address:add']"
        >新增</el-button>
      </el-col>
      
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['order:address:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="addressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户" align="center" prop="userName" />
      <el-table-column label="电话" align="center" prop="userTelephone" />
      <el-table-column label="区域" align="center" prop="area" />
      <el-table-column label="详细地址" align="center" prop="detail" />
      <el-table-column label="经度" align="center" prop="x" />
      <el-table-column label="纬度" align="center" prop="y" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:address:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:address:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单地址对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户" />
        </el-form-item>
        
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="userTelephone">
          <el-input v-model="form.userTelephone" placeholder="请输入电话" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="选择区域" prop="area">
              <el-cascader 
                 :value="form.areaList"
                 :options="areaOptions"
                 @change="areaChange"
                 style="width: 100%;"
                 :show-all-levels="true" :props="props"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="详细地址" prop="detail">
          <el-input v-model="form.detail" placeholder="请输入详细地址" >
            <el-button slot="append" icon="el-icon-search" @click="searchLocation"></el-button>
          </el-input>
        </el-form-item>
          </el-col>
         
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="位置" prop="loc">
              <baidu-map :center="center" :zoom="zoom" @ready="handler" class="map" @click="handlerMapClick">
              <!-- 控件将定位到地图的右上角 -->
              <bm-scale anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-scale>
              <!-- 平移缩放组件 -->
              <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
              <!-- 缩略图 -->
              <bm-overview-map anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :isOpen="true"></bm-overview-map>
              <!-- 定位 -->
              <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_LEFT" :showAddressBar="true"
                              :autoLocation="true"></bm-geolocation>
              <bm-map-type :map-types="['BMAP_NORMAL_MAP', 'BMAP_HYBRID_MAP']"
                          anchor="BMAP_ANCHOR_TOP_LEFT"></bm-map-type>
              <!-- 示例位置 -->
            </baidu-map>
            </el-form-item>            
          </el-col>
         
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="经度" prop="x">
              <el-input v-model="form.x" placeholder="请输入经度" :readonly="true" />
            </el-form-item>        
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="y">
              <el-input v-model="form.y" placeholder="请输入纬度" :readonly="true"  />
            </el-form-item>
          </el-col>
        </el-row>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAddress, getAddress, delAddress, addAddress, updateAddress, exportAddress } from "@/api/order/address";
import { treeArea } from "@/api/system/area";

export default {
  name: "Address",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单地址表格数据
      addressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      areaOptions:[],
      props:{
        value: 'id',
        label: 'name',
        expandTrigger: 'hover'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        userTelephone: null,
        area: null,
        arearCode: null,
        detail: null,
        x: null,
        y: null,
        userId: null
      },
      // 表单参数
      form: {},
     
      // 表单校验
      rules: {
        x: [
          { required: true, message: "经度不能为空", trigger: "blur" }
        ],
        y: [
          { required: true, message: "纬度不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        userTelephone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
      },
      center: {
        lng: 0,
        lat: 0
      },
      zoom: 3,
      map: null,
      BMap: null
    };
  },
  created() {
    this.getList();
    this.getAreaTree();
  },
  methods: {
    /** 查询订单地址列表 */
    getList() {
      this.loading = true;
      listAddress(this.queryParams).then(response => {
        this.addressList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAreaTree() {
      treeArea().then(response => {
        this.areaOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userName: null,
        userTelephone: null,
        area: null,
        arearCode: null,
        detail: null,
        x: null,
        y: null,
        userId: null,
        areaList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加地址";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAddress(id).then(response => {
        this.form = response.data;   
        let areaCode = response.data.areaCode; 
        this.form.areaList = [areaCode.substr(0,2)+"0000", areaCode.substr(0,4)+"00", areaCode].map(a=>parseInt(a));
        this.open = true;
        this.title = "修改地址";
      });
    },
    areaChange(node){
      this.form.areaCode = node[node.length -1 ];
      // this.form.areaCode = this.form.area;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAddress(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAddress(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除地址编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAddress(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有地址数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportAddress(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    },
    // 百度地图
    handler ({BMap, map}) {
      map.centerAndZoom('西安市', 12);
      map.enableScrollWheelZoom(true) // 开启鼠标滚轮缩放
      // this.pt = new BMap.Point(116.117, 39.909)
      // // 中心点
      // this.center.lng = 117.18
      // this.center.lat = 34.27
      // this.zoom = 11.5
      this.BMap = BMap;
      this.map = map;
    },
    searchLocation(){
      var local = new this.BMap.LocalSearch(this.map, {
        renderOptions:{map: this.map}
      });
      local.search(this.form.detail);
    },
    handlerMapClick({type, target, point, pixel, overlay}){
      this.form.x = point.lng;
      this.form.y = point.lat;
    }
  }
};
</script>
<style scoped>
  
  .mapBox1 {
    line-height: 350px;
  }
  .map {
    opacity: 0.9;
    margin-right: 0px;
    height: 350px;
  }
 
</style>